<template>
  <p-modal v-drag
    :title="title"
    :width="width"
    :visible="visible"
    destroyOnClose="true"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    okText="保存"
    @cancel="handleCancel"
    cancelText="取消"
  >
    <p-form-model ref="ruleForm" :model="form" :rules="rules">
      <p-row>
        <p-col :span="8">
          <p-form-model-item
            label="单据编号"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="number"
          >
            <p-input
              readOnly
              v-model="form.number"
              placeholder="请输入单据编号"
            ></p-input>
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="申报日期"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="data"
          >
            <j-date
              readOnly
              placeholder="请选择申报日期"
              v-model="form.data"
              :trigger-change="true"
              style="width: 100%"
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="线体"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="line"
          >
            <p-select v-model="form.line" placeholder="选择线体" :disabled="dataList.change">
              <p-select-option
                @click="changeLine(item)"
                :value="item.linename"
                v-for="item in lineList"
                :key="item.id"
                >{{ item.linename }}
              </p-select-option>
            </p-select>
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="班次"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="Frequency"
          >
            <p-select v-model="form.Frequency" placeholder="选择班次" :disabled="dataList.change">
              <p-select-option
                @click="changeClass(item)"
                :value="item.shiftname"
                v-for="item in classList"
                :key="item.id"
                >{{ item.shiftname }}
              </p-select-option>
            </p-select>
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="班组"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="classno"
          >
            <p-input
              readOnly
              v-model="form.classno"
              placeholder="请输入班组"
            ></p-input>
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="申报车间"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="work"
          >
            <p-input
              readOnly
              v-model="form.work"
              placeholder="请输入申报车间"
            ></p-input>
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="生产订单号"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="order"
          >
            <p-input
              v-model="form.order"
              @blur="getMcode"
              placeholder="请输入生产订单号"
              :disabled="dataList.change"
            ></p-input>
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="产品编码"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="mcode"
          >
            <p-input
              readOnly
              v-model="form.mcode"
              placeholder="请输入产品编码"
            ></p-input>
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="产品名称"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="mname"
          >
            <p-input
              readOnly
              v-model="form.mname"
              placeholder="请输入产品名称"
            ></p-input>
          </p-form-model-item>
        </p-col>
        <p-col :span="20" :pull="4">
          <p-form-model-item
            label="备注"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
           
            prop="text"
          >
            <p-input placeholder="备注" v-model="form.text"  :disabled="dataList.change"></p-input>
          </p-form-model-item>
        </p-col>
      </p-row>
    </p-form-model>

    <MesHrMonthlyEdDetailList
      :destroyOnClose="true"
      ref="listData"
      :id="id"
      :change="dataList.change"
    ></MesHrMonthlyEdDetailList>
    <TechnologicaPprocess :id="dataList.processInstanceId" v-if="dataList.processInstanceId"></TechnologicaPprocess>
  </p-modal>
</template>

<script>
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import {
  postAction,
  getAction,
  deleteAction,
  putAction,
  api,
} from "@/api/manage";
import pick from "lodash.pick";
import JDate from "@/components/jeecg/JDate";
import JUpload from "@/components/jeecg/JUpload";
import JDictSelectTag from "@/components/dict/JDictSelectTag";
import TechnologicaPprocess from "@/views/work/components/TechnologicaPprocess";
import MesHrMonthlyEdDetailList from "./MesHrMonthlyEdDetailList";
import moment from "moment";

export default {
  name: "MesHrMonthlyEmpDeclareModal",
  mixins: [SubmitOnchangMixin],
  components: {
    JDate,
    JUpload,
    JDictSelectTag,
    MesHrMonthlyEdDetailList,
    TechnologicaPprocess
  },
  data() {
    return {
      dataList: {},
      isShowDownload: false,
      fileList: [],
      lineList: [],
      classList: [],
      confirmLoading: false,
      linkData: {},
      Datalist: {},
      getlist: [],
      form: {
        number: "",
        data: moment(new Date(), "YYYY-MM-DD"),
        line: "",
        Frequency: "",
        Frequencycode: "",
        classno: "",
        work: "",
        order: "",
        mcode: "",
        mname: "",
        text: "",
      },
      data: {
        bucketName: "mes",
        expireDate: 0,
      },
      moment,
      title: "新增",
      width: "70%",
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      rules: {
        month: [{ required: true, message: "请选择月份" }],
        line: [{ required: true, message: "请选择线体" }],
        Frequency: [{ required: true, message: "请选择班次" }],
        order: [{ required: true, message: "请输入生产订单号" }],
      },
      url: {
        add: "/mes-manpower/mesHrSpecialEmpManhourDeclare",
        edit: "/mes-manpower/mesHrSpecialEmpManhourDeclare",
        upload: api.test + "/poros-oss/file/upload",
        getmcode: "/pmc-plan/productionno/query/",
        getClassline: "/mes-manpower/mesHrClassLineInfo/classLineInfoList", //获取班组线体
        getclass: "/basic-business/mesBaShift/dtolist", //获取班次信息
      },
    };
  },
  watch: {
    dataList() {
      console.log(this.dataList)
      if (Object.keys(this.dataList).length > 1) {
        this.analysis();
      } else if (Object.keys(this.dataList).length == 1) {
        this.ifADD();
      }
      console.log(this.dataList);
    },
  },
  created() {},
  mounted() {
    this.getClassline();
  },
  methods: {
    getMcode() {
      if (this.form.order) {
        getAction(this.url.getmcode + this.form.order).then((res) => {
          if (res.code == 0) {
            this.$set(this.form, "mcode", res.data.mcode);
            this.$set(this.form, "mname", res.data.mname);
            this.Datalist = res.data;
          } else {
            this.$message.error(res.msg);
          }
        });
      }
    },
    getClassline() {
      getAction(this.url.getClassline).then((res) => {
        if (res.code == 0) {
          this.lineList = res.data;
        }
      });
      getAction(this.url.getclass).then((res) => {
        if (res.code == 0) {
          this.classList = res.data;
        }
      });
    },
    changeLine(val) {
      this.$set(this, "linkData", val);
      this.$set(this.form, "line", val.lineno);
      this.$set(this.form, "work", val.workroomname);
      this.$set(this.form, "classno", val.classname);
    },
    changeClass(val) {
      this.$set(this.form, "Frequency", val.shiftname);
      this.$set(this.form, "Frequencycode", val.shiftno);
    },

    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "applyNo",
            "declareDate",
            "mesYearMonth",
            "approveStatus",
            "declareReason",
            "fileUrl",
            "createBy",
            "createTime",
            "updateBy",
            "updateTime",
            "remark"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    analysis() {
      const {
        orderNo: number,
        declareDate: data,
        linename: line,
        shiftName: Frequency,
        declareClassname: classno,
        declareWorkroom: work,
        productionNo: order,
        shift: Frequencycode,
        mcode,
        mname,
        remark: text,
      } = this.dataList;
      this.form = {
        number,
        data: moment(data),
        line,
        Frequency,
        Frequencycode,
        classno,
        work,
        order,
        mcode,
        mname,
        text,
      };
      this.linkData = {
        lineno: this.dataList.lineno,
        linename:this.dataList.linename,
        classno: this.dataList.classno,
        classname: this.dataList.declareClassname,
        workroomname: this.dataList.declareWorkroom,
        workroomno: this.dataList.workroomno,
      };
      this.id=this.dataList.id
    },
    ifADD() {
      let param = {
        commonFormData: {},
        rules: [
          { ruleCode: "mes_Special_working", columnName: "labelTemplateNo" },
        ],
      };
      this.id=''
      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.code == 0) {
          console.log(res.data.labelTemplateNo)
          this.form = {
            data: moment(new Date()).format("YYYY-MM-DD"),
            month: moment(new Date()).format("MM") + "月",
          },
            this.$set(this.form, "number", res.data.labelTemplateNo);
          this.isShowDownload = false;
        }
      });
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let obj = {
            orderNo: this.form.number, //	单据编号
            declareDate: this.form.data, //	申报日期
            linename: this.linkData.linename, //	线体
            lineno: this.linkData.lineno, //	线体编码
            shift: this.form.Frequencycode, //班次
            shiftName: this.form.Frequency, //班次名称
            classno: this.linkData.classno, //班组编码
            declareClassname: this.linkData.classname, //	申报班组
            declareWorkroom: this.linkData.workroomname, //申报车间
            workroomno: this.linkData.workroomno, //车间编码
            productionNo: this.form.order, //	生产工单
            mcode: this.form.mcode, //产品编码
            mname: this.form.mname, //	产品名称
            remark: this.form.text, //	备注
            detailAddParamList: this.$refs.listData.dataSource,
          };
          // 判断是否新增
          if (Object.keys(this.dataList).length == 1) {
            this.confirmLoading = true;
            postAction(this.url.add, obj).then((res) => {
              if (res.code == 0) {
                this.$message.success("保存成功");
                this.$parent.$parent.loadData();
                this.visible = false;
              } else {
                this.$message.error(res.msg); 
              }
              this.confirmLoading = false;
            });
          } else {
            // 判断单据状态
            if (
              this.dataList.approveStatus == 0 ||
              this.dataList.approveStatus == 2
            ) {
              this.confirmLoading = true;
              obj.id=this.dataList.id
              putAction(this.url.add, obj).then((res) => {
                if (res.code == 0) {
                  this.$message.success("修改成功");
                  this.$parent.$parent.loadData();
                  this.visible = false;
                } else {
                  this.$message.error(res.msg);
                }
                 this.confirmLoading = false;
              });
            } else {
              this.$message.error("该状态不能修改");
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "applyNo",
          "declareDate",
          "mesYearMonth",
          "approveStatus",
          "declareReason",
          "fileUrl",
          "createBy",
          "createTime",
          "updateBy",
          "updateTime",
          "remark"
        )
      );
    },
  },
};
</script>